import request from "@/utils/request";

export interface UserVO {
  id: number;
  username: string;
  nickname: string;
  deptId: number;
  postIds: string[];
  email: string;
  mobile: string;
  sex: number;
  avatar: string;
  loginIp: string;
  status: number;
  remark: string;
  loginDate: Date;
  createTime: Date;
}

export interface UserConfigVO {
  /**
   * 配置键
   */
  configKey?: string;
  /**
   * 配置值
   */
  configValue?: string;
  /**
   * 创建时间
   */
  createTime?: string[];
  /**
   * 页码，从 1 开始
   */
  pageNo?: number;
  /**
   * 每页条数，最大值为 100
   */
  pageSize?: number;
}

// 查询用户管理列表
export const getUserPage = (params: PageParam) => {
  return request.get({ url: "users/page", params });
};

// 查询所有用户列表
export const getAllUser = () => {
  return request.get({ url: "users/all" });
};

// 查询用户详情
export const getUser = (id: number) => {
  return request({ url: "users/info" + id, method: "get" });
};

// 新增用户
export const createUser = (data: UserVO) => {
  return request({ url: "users", data, method: "post" });
};

// 修改用户
export const updateUser = (id: number, data: UserVO) => {
  return request({ url: `users/${data.id}`, data, method: "put" });
};

// 删除用户
export const deleteUser = (id: number) => {
  return request.delete({ url: "users/delete?id=" + id });
};

export const deleteByIdsUser = (ids: string) => {
  return request({
    url: `users/${ids}`,
    method: "delete",
  });
};

/**
 * 修改用户密码
 *
 * @param id 用户ID
 * @param password 新密码
 */
export const resetPasswordUser = (id: string, password: string) => {
  return request({
    url: `users/${id}/password/reset`,
    method: "put",
    params: { password },
  });
};

// 导出用户
export const exportUser = (params: any) => {
  const USER_BASE_URL = "/api/v1/users";

  return request({
    url: `${USER_BASE_URL}/export`,
    method: "get",
    params,
    responseType: "blob",
    downloadType: "server",
  });
};

export const getRoleList = (data?: any) => {
  const roleList = ref([
    {
      id: 1,
      name: "统计员",
      desc: "查看和导出各类统计数据",
    },

    { id: 2, name: "查看员", desc: "仅能查看基本信息，无操作权限" },

    {
      id: 3,
      name: "人事管理",
      desc: "人员招聘、入职、离职管理等",
    },
  ]);
  roleList.value.forEach((item: any) => {
    item.value = item.id;
    item.label = item.name + `(${item.desc})`;
  });
  if (data) {
    data.value = roleList.value;
  }
  return roleList;
};
